﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace GP2D3D_Homework1_RickBeijer_S1012261
{
    public partial class DisplayModeSelectionDialog : Form
    {
        /// <summary>
        /// All display modes possible for the user to select.
        /// </summary>
        public List<CustomDisplayMode> DisplayModes
        {
            get;
            set;
        }

        /// <summary>
        /// After this dialog has closed, this will contain the display mode selected by the user.
        /// </summary>
        public CustomDisplayMode SelectedDisplayMode
        {
            get;
            set;
        }

        /// <summary>
        /// After this dialog has closed, this will contain the index of the display mode selected by
        /// the user, derived from the DisplayModes list.
        /// </summary>
        public int SelectedDisplayModeIndex
        {
            get;
            set;
        }

        public DisplayModeSelectionDialog(List<CustomDisplayMode> displayModes, int currentDisplayModeIndex)
        {
            InitializeComponent();

            this.DisplayModes = displayModes;

            lvDisplayModes.Columns.Add("DeviceName", 80);
            lvDisplayModes.Columns.Add("PixelDepth", 65);
            lvDisplayModes.Columns.Add("Resolution", 72);
            lvDisplayModes.Columns.Add("RefreshRate", 75);

            foreach (CustomDisplayMode displayMode in DisplayModes)
            {
                lvDisplayModes.Items.Add(new ListViewItem(displayMode.ToStringArray()));
            }

            lvDisplayModes.Items[currentDisplayModeIndex].ForeColor = Color.Navy;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            GetSelectedItemAndSetDialogResult();
        }

        private void lvDisplayModes_DoubleClick(object sender, EventArgs e)
        {
            GetSelectedItemAndSetDialogResult();

            if (DialogResult == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void GetSelectedItemAndSetDialogResult()
        {
            if (lvDisplayModes.SelectedItems.Count > 0)
            {
                SelectedDisplayModeIndex = lvDisplayModes.SelectedItems[0].Index;
                SelectedDisplayMode = DisplayModes[SelectedDisplayModeIndex];

                DialogResult = DialogResult.OK;
            }
        }
    }
}
